home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / qndxr.2 ƒ / file_utils.2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-11-03  |  3.2 KB  |  122 lines  |  [TEXT/KAHL]

  1. /* file file_utils.c ... by ^z -- 870820-0913-...
  2.  * some utility routines for qndxr project, associated with files...
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <unix.h>
  7. #include <storage.h>
  8. #include <strings.h>
  9. #include <ctype.h>
  10. #include <proto.h>
  11. #include "qndxr.2.h"
  12.  
  13. /* function to write out sorted k & p files based on the doc and ptr
  14.  * arrays in memory....
  15.  *
  16.  * The kfile format is as described in detail elsewhere:
  17.  *    the key word, turned into all capital letters and with spaces
  18.  *        afterward, of fixed length KEY_LENGTH; and
  19.  *    the cumulative count of how many words have passed before, including
  20.  *        the current word, a long integer.
  21.  *
  22.  * Function revised 870907-... by ^z to use zbuffer method....
  23.  */
  24.  
  25. void write_sorted_files (doc, ptr, nwords, pass_number, offset)
  26.   char *doc, **ptr;
  27.   long nwords, offset;
  28.   int pass_number;
  29.   {
  30.     extern long zbufsiz;
  31.     FILE *kfile, *pfile, *open_kfile(), *open_pfile();
  32.     char *prev_word, *next_output_item();
  33.     KEY_REC *outk;
  34.     long *outp, i, file_size ();
  35.     void create_zbuffer(), write_new_key();
  36.     
  37.     DEBUG ("--Entering write_sorted_files with nwords %ld\n", nwords);
  38.     if (nwords == 0)
  39.         return;
  40.     
  41.     DEBUG ("--Opening kfile & pfile for pass_number = %d\n", pass_number);
  42.     kfile = open_kfile (pass_number);
  43.     pfile = open_pfile (pass_number);
  44.     
  45.     DEBUG ("--Creating buffers for keys & ptrs, size = %ld\n", zbufsiz);
  46.     create_zbuffer (0, zbufsiz, kfile, sizeof(KEY_REC));
  47.     create_zbuffer (1, zbufsiz, pfile, sizeof(long));
  48.  
  49.     DEBUG ("--Beginning to write keys and ptrs; first key=%.28s\n", ptr[0]);
  50.     prev_word = ptr[0];
  51.     outk = (KEY_REC *)next_output_item (0);
  52.     write_new_key (ptr[0], outk->kkey);
  53.     
  54.     for (i = 0; i < nwords; ++i)
  55.       {
  56.         if (is_new_word (prev_word, ptr[i]))
  57.           {
  58.             outk->ccount = i;
  59.             outk = (KEY_REC *)next_output_item (0);
  60.             write_new_key (ptr[i], outk->kkey);
  61.             prev_word = ptr[i];
  62.           }
  63.         outp = (long *)next_output_item (1);
  64.         *outp = (ptr[i] - doc) + offset;
  65.       }
  66.     outk->ccount = i;
  67.  
  68.     flush_zbuffer (0);
  69.     flush_zbuffer (1);
  70.     
  71.     DEBUG ("--Getting rid of key and ptr buffers...\n", NULL);
  72.     free_zbuffer (0);
  73.     free_zbuffer (1);
  74.     
  75.     printf (" ...%ld distinct words\n",
  76.             file_size (kfile) / sizeof(KEY_REC));
  77.     fclose (kfile);
  78.     fclose (pfile);
  79.   }
  80.  
  81.  
  82. /* function to determine if the current word is the same as or different
  83.  * from the previous word -- if it is different, we'll need to write an
  84.  * entry out to the key file kfile -- compare the words up to the first
  85.  * '\0', or for a maximum distance of KEY_LENGTH, and return TRUE
  86.  * if they differ, FALSE if they are identical that far.  Thus, a simple
  87.  * call to zstrcmp() does the job.... but keep ours as a function instead
  88.  * of a macro call for the moment, for safety and readability....
  89.  */
  90.  
  91. int is_new_word (w0, w1)
  92.   char *w0, *w1;
  93.   {
  94.     return (zstrcmp (w0, w1));
  95.   }
  96.  
  97.  
  98. /* function to write out a new key entry in the key_file:
  99.  * KEY_LENGTH letters consisting of the key word (which will be found
  100.  * delimited by a '\0'), followed by enough blanks to fill out the
  101.  * record to total length KEY_LENGTH ...
  102.  */
  103.  
  104. void write_new_key (p, kp)
  105.   register char *p, *kp;
  106.   {
  107.     register int i, c;
  108.     
  109.     for (i = 0; i < KEY_LENGTH; ++i)
  110.       {
  111.         c = *p++;
  112.         if (c == '\0')
  113.             break;
  114.         *kp++ = c;
  115.       }
  116.  
  117.     for ( ; i < KEY_LENGTH; ++i)
  118.         *kp++ = ' ';
  119.   }
  120.  
  121.  
  122.